<?php

App::uses('AppController', 'Controller');

class AdTypesController extends AppController {

    public $pageTitle = "Tipovi reklama";

    public $uses = array('AdType');
    
    public $paginate = array (
        'limit' => ITEMS_PER_PAGE
    );

    public function index() {
        $filter = $this->parseFilterData('AdType');
        
        $this->paginate['order'] = "{$filter->sortBy} {$filter->sortDir}";
        $ad_types = $this->paginate('AdType', $filter->conditions);
        
        $this->set('ad_types', $ad_types);
        $this->setFilterData($filter);
    }
    
    public function create() {
        $this->set('data', $this->AdType->create());
        $this->render('form');
    }
    
    public function edit($id) {
        $this->set('data', $this->AdType->findById($id));
        $this->render('form');
    }
    
    public function save() {
        $data = $this->data;
        if (! empty($data)) {
            $this->AdType->create($data);
            
            if (! $this->AdType->validates()) {
                $this->set('data', $data);
                $this->setFlash('Uneti podaci nisu validni.');
                return $this->render('form');
            }
            
            if (! $this->AdType->save($data)) {
                $this->set('data', $data);
                $this->setFlash('Greška pri čuvanju podataka.');
                return $this->render('form');
            }
            
            $this->setFlash('Tip reklame je uspešno sačuvan.', 'ok');
            $this->redirect('edit/'.$this->AdType->id);
        }
        
        $this->redirect('index');
    }
    
    public function remove($id) {
        if ($this->AdType->delete($id)) {
            $this->setFlash('Tip reklame je obrisan.', 'ok');
        } else {
            $this->setFlash('Tip reklame nije obrisan.');
        }
        
        $this->redirect('index');
    }
    
}
